package cate.game.pay.msg;

import cate.common.table.pay.PayItemRow;
import cp.solution.business.sys.app.GameAppEntity;
import cp.external.yzx.pay.YzxClientOrder;
import cp.solution.business.pay.PayTodoEntity;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBPay;
import cate.game.event.pay.PayOrderInitEvent;
import cate.game.pay.PayDataAssembler;
import cate.game.role.Role;
import cate.game.role.pay.PayCheck;
import easy.java.codec.JsonCodec;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ClientMsg(title = "客户端构建支付订单 の 请求")
public class PayOrderBuildReq implements MsgInput {

    @NoteField("支付项模板ID")
    public int itemTid;

    @NoteField(value = "数量")
    public int num;

    @NoteField(value = "叶子戏执行版本", detail = "0就是最早版本，1就是吴伟文邀请更换的最新版SDK")
    public int version;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        //cc.getRole().getPay();
        h(game, cc.getRole());
    }

    @Override
    public MsgConfig conf() {
        return MsgIdBPay.ORDER_BUILD_REQ;
    }

    public void h(GameBody game, Role role) {
        this.version = 1;
        if(role.getBase().level < 8) {
            game.notice.message(role, "支付功能需8级后才可开启");
            return;
        }
        //log.info(role.getPay().note);
        //role.getPay().note = "xx";
        PayItemRow pir = game.table.pay.item.get(this.itemTid);
        if(pir == null) {
            game.notice.message(role, "找不到ID="+this.itemTid+"的支付项");
            return;
        }

        EcResult rck = PayCheck.allow(pir, role);
        if(!rck.ok()) {
            game.notice.message(role, rck.message);
            return;
        }

        PayTodoEntity todo = PayDataAssembler.buildTodo(game, role, pir, this.num);
        try {
            GameAppEntity app = game.apisLoader().console.gameApp.getByCode(role.getBase().gameId, true);

            PayTodoEntity todoResp = game.apis().pay.core.buildTodo(todo);
            YzxClientOrder order = PayDataAssembler.buildClientOrder(todoResp, app.secretKey, version);
            log.info("生成给客户端的 order json 数据={}", JsonCodec.instance().encodeString(order));
            role.getGame().eventDispatcher.dispatchEvent(new PayOrderInitEvent(role, todoResp));
            PayOrderBuildResp resp = new PayOrderBuildResp();
            resp.order = order;
            role.sendNow(resp);
        } catch (Exception e) {
            game.notice.message(role, e.getMessage());
            log.error("", e);
        }
    }

    public void forGmH(GameBody game, Role role) {
        this.version = 1;
        if(role.getBase().level < 8) {
            game.notice.message(role, "支付功能需8级后才可开启");
            return;
        }
        //log.info(role.getPay().note);
        //role.getPay().note = "xx";
        PayItemRow pir = game.table.pay.item.get(this.itemTid);
        if(pir == null) {
            game.notice.message(role, "找不到ID="+this.itemTid+"的支付项");
            return;
        }

//        EcResult rck = PayCheck.allow(pir, role);
//        if(!rck.ok()) {
//            game.notice.message(role, rck.message);
//            return;
//        }

        PayTodoEntity todo = PayDataAssembler.buildTodo(game, role, pir, this.num);
        try {
            GameAppEntity app = game.apisLoader().console.gameApp.getByCode(role.getBase().gameId, true);

            PayTodoEntity todoResp = game.apis().pay.core.buildTodo(todo);
            YzxClientOrder order = PayDataAssembler.buildClientOrder(todoResp, app.secretKey, version);
            log.info("生成给客户端的 order json 数据={}", JsonCodec.instance().encodeString(order));
            role.getGame().eventDispatcher.dispatchEvent(new PayOrderInitEvent(role, todoResp));
            PayOrderBuildResp resp = new PayOrderBuildResp();
            resp.order = order;
            role.sendNow(resp);
        } catch (Exception e) {
            game.notice.message(role, e.getMessage());
            log.error("", e);
        }
    }
}
